<%@ page contentType="text/html;charset=UTF-8" %>
<script>
$(document).ready(function() {
    $('#date').datetimepicker({
        format: "YYYY-MM-DD HH:00"
    });
    $('#date1').datetimepicker({
        format: "YYYY-MM-DD HH:00"
    });
    $('#date2').datetimepicker({
        format: "YYYY-MM-DD HH:00"
    });
	$('#dispatchingTable').bootstrapTable({
		 
		  //请求方法
               method: 'get',
               //类型json
               dataType: "json",
               //显示刷新按钮
               showRefresh: true,
               //显示切换手机试图按钮
               showToggle: true,
               //显示 内容列下拉框
    	       showColumns: true,
    	       //显示到处按钮
    	       showExport: true,
    	       //显示切换分页按钮
    	       showPaginationSwitch: true,
    	       //最低显示2行
    	       minimumCountColumns: 2,
               //是否显示行间隔色
               striped: true,
               //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）     
               cache: false,    
               //是否显示分页（*）  
               pagination: true,   
                //排序方式 
               sortOrder: "asc",  
               //初始化加载第一页，默认第一页
               pageNumber:1,   
               //每页的记录行数（*）   
               pageSize: 10,  
               //可供选择的每页的行数（*）    
               pageList: [10, 25, 50, 100],
               //这个接口需要处理bootstrap table传递的固定参数,并返回特定格式的json数据  
               url: "${ctx}/tms/dispatching/data1",
               //默认值为 'limit',传给服务端的参数为：limit, offset, search, sort, order Else
               //queryParamsType:'',   
               ////查询参数,每次调用是会带上这个参数，可自定义                         
               queryParams : function(params) {
               	var searchParam = $("#searchForm").serializeJSON();
               	searchParam.pageNo = params.limit === undefined? "1" :params.offset/params.limit+1;
               	searchParam.pageSize = params.limit === undefined? -1 : params.limit;
               	searchParam.orderBy = params.sort === undefined? "" : params.sort+ " "+  params.order;
                   return searchParam;
               },
               //分页方式：client客户端分页，server服务端分页（*）
               sidePagination: "server",
               contextMenuTrigger:"right",//pc端 按右键弹出菜单
               contextMenuTriggerMobile:"press",//手机端 弹出菜单，click：单击， press：长按。
               contextMenu: '#context-menu',
               onContextMenuItem: function(row, $el){
                   if($el.data("item") == "edit"){
                   	edit(row.id);
                   } else if($el.data("item") == "delete"){
                        jp.confirm('确认要删除该派车单记录吗？', function(){
                       	jp.loading();
                       	jp.get("${ctx}/tms/dispatching/delete?id="+row.id, function(data){
                   	  		if(data.success){
                   	  			$('#dispatchingTable').bootstrapTable('refresh');
                   	  			jp.success(data.msg);
                   	  		}else{
                   	  			jp.error(data.msg);
                   	  		}
                   	  	})
                   	   
                   	});
                      
                   } 
               },


               onClickRow: function(row, $el){
               },

               columns: [{
		        checkbox: true
		       
		    },{
                   field: 'pro',
                   title: '项目',
                   sortable: true

               } ,{
                   field: 'hth',
                   title: '合同号',
                   sortable: true

               },{
                   field: 'skr',
                   title: '收款人',
                   sortable: true

               }
                   ,{
                       field: 'kh',
                       title: '卡号',
                       sortable: true

                   }
                   ,{
                       field: 'khh',
                       title: '开户行',
                       sortable: true

                   }
                   ,{
                       field: 'zffs',
                       title: '支付方式',
                       sortable: true

                   } ,{
                       field: 'zyf',
                       title: '总运费',
                       sortable: true

                   },{
                       field: 'yf',
                       title: '已付',
                       sortable: true

                   }
                   ,{
                       field: 'wf',
                       title: '未付',
                       sortable: true

                   }
                   ,{
                       field: 'zxj',
                       title: '总现金',
                       sortable: true

                   },{
                       field: 'yfxj',
                       title: '已付现金',
                       sortable: true

                   }
                   ,{
                       field: 'wfxj',
                       title: '未付现金',
                       sortable: true

                   }
                   ,{
                       field: 'zyk',
                       title: '总油卡',
                       sortable: true

                   },{
                       field: 'yfyk',
                       title: '已付油卡',
                       sortable: true

                   }
                   ,{
                       field: 'wfyk',
                       title: '未付油卡',
                       sortable: true

                   }
                   ,{
                       field: 'zhfk',
                       title: '装货付款',
                       sortable: true

                   },{
                       field: 'hdfk',
                       title: '货到付款',
                       sortable: true

                   },{
                       field: 'hzjs',
                       title: '回执结算',
                       sortable: true

                   },{
                       field: 'zhfkxj',
                       title: '装货付款现金',
                       sortable: true

                   },{
                       field: 'zhfkyk',
                       title: '装货付款油卡',
                       sortable: true

                   }
                   ,{
                       field: 'ykkh',
                       title: '油卡卡号(装货)',
                       sortable: true

                   }
                   ,{
                       field: 'hdfkxj',
                       title: '货到付款现金',
                       sortable: true

                   },{
                       field: 'hdfkyk',
                       title: '货到付款油卡',
                       sortable: true

                   }
                   ,{
                       field: 'hdykkh',
                       title: '油卡卡号(货到)',
                       sortable: true

                   }
                   ,{
                       field: 'hzfkxj',
                       title: '回执付款现金',
                       sortable: true

                   },{
                       field: 'hzfkyk',
                       title: '回执付款油卡',
                       sortable: true

                   }
                   ,{
                       field: 'hzykkh',
                       title: '油卡卡号(回执)',
                       sortable: true

                   }
		    ,{
                   field: 'pcdh',
                   title: '派车单号',
                   sortable: true

               },{
                       field: 'zjs',
                       title: '总件数',
                       sortable: true

                   }
                   ,{
                       field: 'zzl',
                       title: '总重量',
                       sortable: true

                   }



			,{
		        field: 'cys',
		        title: '承运商',
		        sortable: true
		       
		    }
			,{
		        field: 'cyslxr',
		        title: '承运商联系人',
		        sortable: true
		       
		    }
			,{
		        field: 'cysdh',
		        title: '承运商电话',
		        sortable: true
		       
		    }
			,{
		        field: 'cph',
		        title: '车牌号',
		        sortable: true
		       
		    }
			,{
		        field: 'cllx',
		        title: '车辆类型',
		        sortable: true
		       
		    }
			,{
		        field: 'xszh',
		        title: '行驶证号',
		        sortable: true
		       
		    }
			,{
		        field: 'sjxm',
		        title: '司机姓名',
		        sortable: true
		       
		    }
			,{
		        field: 'sjdh',
		        title: '司机电话',
		        sortable: true
		       
		    }
			,{
		        field: 'jszh',
		        title: '驾驶证号',
		        sortable: true
		       
		    }



		     ]
		
		});
		
		  
	  if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){//如果是移动端

		 
		  $('#dispatchingTable').bootstrapTable("toggleView");
		}
	  
	  $('#dispatchingTable').on('check.bs.table uncheck.bs.table load-success.bs.table ' +
                'check-all.bs.table uncheck-all.bs.table', function () {
            $('#remove').prop('disabled', ! $('#dispatchingTable').bootstrapTable('getSelections').length);
            $('#edit').prop('disabled', $('#dispatchingTable').bootstrapTable('getSelections').length!=1);
          $('#scht').prop('disabled', $('#dispatchingTable').bootstrapTable('getSelections').length!=1);
          $('#fc').prop('disabled', $('#dispatchingTable').bootstrapTable('getSelections').length!=1);
          $('#ckd').prop('disabled', $('#dispatchingTable').bootstrapTable('getSelections').length!=1);
          $('#qxpz').prop('disabled', $('#dispatchingTable').bootstrapTable('getSelections').length!=1);
          $('#qxht').prop('disabled', $('#dispatchingTable').bootstrapTable('getSelections').length!=1);
          $('#qxck').prop('disabled', $('#dispatchingTable').bootstrapTable('getSelections').length!=1);

      });
		  
		$("#btnImport").click(function(){
			jp.open({
			    type: 1, 
			    area: [500, 300],
			    title:"导入数据",
			    content:$("#importBox").html() ,
			    btn: ['下载模板','确定', '关闭'],
				    btn1: function(index, layero){
					  window.location='${ctx}/tms/dispatching/import/template';
				  },
			    btn2: function(index, layero){
				        var inputForm =top.$("#importForm");
				        var top_iframe = top.getActiveTab().attr("name");//获取当前active的tab的iframe 
				        inputForm.attr("target",top_iframe);//表单提交成功后，从服务器返回的url在当前tab中展示
				        inputForm.onsubmit = function(){
				        	jp.loading('  正在导入，请稍等...');
				        }
				        inputForm.submit();
					    jp.close(index);
				  },
				 
				  btn3: function(index){ 
					  jp.close(index);
	    	       }
			}); 
		});
		    
	  $("#search").click("click", function() {// 绑定查询按扭
		  $('#dispatchingTable').bootstrapTable('refresh');
		});
	 
	 $("#reset").click("click", function() {// 绑定查询按扭
		  $("#searchForm  input").val("");
		  $("#searchForm  select").val("");
		  $("#searchForm  .select-item").html("");
		  $('#dispatchingTable').bootstrapTable('refresh');
		});
		
		
	});
		
  function getIdSelections() {
        return $.map($("#dispatchingTable").bootstrapTable('getSelections'), function (row) {
            return row.id
        });
    }
function gethth() {
    return $.map($("#dispatchingTable").bootstrapTable('getSelections'), function (row) {
        return row.hth
    });
}
function getcph() {
    return $.map($("#dispatchingTable").bootstrapTable('getSelections'), function (row) {
        return row.cph
    });
}
function getpcdh() {
    return $.map($("#dispatchingTable").bootstrapTable('getSelections'), function (row) {
        return row.pcdh
    });
}
function getzcxyh() {
    return $.map($("#dispatchingTable").bootstrapTable('getSelections'), function (row) {
        return row.zcxyh
    });
}
function getstatus() {
    return $.map($("#dispatchingTable").bootstrapTable('getSelections'), function (row) {
        return row.status
    });
}
  function deleteAll(){

		jp.confirm('确认要删除该派车单记录吗？', function(){
			jp.loading();  	
			jp.get("${ctx}/tms/dispatching/deleteAll?ids=" + getIdSelections(), function(data){
         	  		if(data.success){
         	  			$('#dispatchingTable').bootstrapTable('refresh');
         	  			jp.success(data.msg);
         	  		}else{
         	  			jp.error(data.msg);
         	  		}
         	  	})
          	   
		})
  }
function qxpz1(){
    if(getstatus()=="已生成合同"||getstatus()=="已发车"){
        return;
    }
    $.ajax({
        url:'${ctx}/tms/dispatching/qxpz?id='+getIdSelections(),
        success:function (data) {
            jp.success(data.msg);
            $('#dispatchingTable').bootstrapTable('refresh');
        },
        error:function () {
            jp.error("出错了")
        }
    })


}
function qxck(){
    if(getstatus()!="已生成出库单"){
        return;
    }
    $.ajax({
        url:'${ctx}/tms/dispatching/qxck?id='+getIdSelections(),
        success:function (data) {
            jp.success(data.msg);
            $('#dispatchingTable').bootstrapTable('refresh');
        },
        error:function () {
            jp.error("出错了")
        }
    })


}
function qxht1(){
    if(getstatus()!="已生成合同"){
        return;
    }
    $.ajax({
        url:'${ctx}/tms/dispatching/qxht?id='+getIdSelections(),
        success:function (data) {
            jp.success(data.msg);
            $('#dispatchingTable').bootstrapTable('refresh');
        },
        error:function () {
            jp.error("出错了")
        }
    })


}
function ckd(){
if(getstatus()!="已配载"){
    return;
}
    $.ajax({
        url:'${ctx}/tms/dispatching/cklist',
        success:function (data) {
            $("#detil").empty();
            $.each($.parseJSON(data), function (index,term) {
                var addTr = $("<tr id='" + term.id + "'></tr>");
                addTr.appendTo("#detil");
                addTr.append("<td><input type='checkbox' name='checkbox'  id='"+term.id+"'    /></td>");
                addTr.append($('<td></td>').text(term.store));
            })
        },
        error:function () {
            jp.error("出错了")
        }
    })

    $("#scckd").modal("show");
}
function bh(val) {
    if($(val).is(":checked")){
        $("[name='checkbox' ]").each(function () {
            $(this).attr("checked", true);
        })
    }else{
        $("[name='checkbox' ]").each(function () {
            $(this).attr("checked", false);
        })
    }
}
function ckqd() {
    $("[name='checkbox' ]").each(function () {
        if($(this).is(":checked")){
            var id=this.id;
            $.ajax({
                dataType:'json',
                async:false,
                url:'${ctx}/tms/dispatching/ckd?i='+id+"&id="+getIdSelections(),
                success:function (data) {
                    jp.success("生成出库单成功")

                }})

        }
    })
    $("#scckd").modal("hide");
    $('#dispatchingTable').bootstrapTable('refresh');
}
function scht(){
    if(getstatus()!="已装车"){
        return;
    }
    jp.confirm('确认要生成合同吗？', function(){
        jp.loading();
        jp.get("${ctx}/tms/dispatching/scht?id=" + getIdSelections(), function(data){
            if(data.success){
                $('#dispatchingTable').bootstrapTable('refresh');
                jp.success(data.msg);
            }else{
                jp.error(data.msg);
            }
        })

    })
}
function fc(){
    if(getstatus()!="已生成合同"){
        return;
    }
	$("#zcxyh").val(getzcxyh());
    $("#hth").val(gethth());
    $("#pcdh").val(getpcdh());
    $("#cph").val(getcph());
	$("#fctx").modal("show");
}
function fcqd(){
   var date= $("#dd").val();
    var date1=$("#lk").val();
    var date2=$("#yjdd").val();
    $("#fctx").modal("hide");
        jp.get("${ctx}/tms/dispatching/fc?id=" + getIdSelections()+"&dd="+date+"&lk="+date1+"&yjdd="+date2, function(data){
            if(data.success){
                $('#dispatchingTable').bootstrapTable('refresh');
                jp.success(data.msg);
            }else{
                jp.error(data.msg);
            }
        })
}
   function add(){
	  jp.openDialog('新增派车单', "${ctx}/tms/dispatching/form",'1200px', '1000px', $('#dispatchingTable'));
  }
  function edit(id){//没有权限时，不显示确定按钮
  	  if(id == undefined){
			id = getIdSelections();
		}
      if(getstatus()!="已配载"&&getstatus()!="已出库"){
          return;
      }
	  jp.openDialog('编辑派车单', "${ctx}/tms/dispatching/form1?id=" + id,'1200px', '1000px', $('#dispatchingTable'));

  }
  function view(id) {
      jp.openDialogView('编辑派车单', "${ctx}/tms/dispatching/form1?id=" + id,'1200px', '1000px', $('#dispatchingTable'));

  }

</script>